/*
 *
 */
package org.jiscinvolve.astrodabis.barberry.registry;

import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;  

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.net.URI;
import java.net.URL;
import java.net.MalformedURLException;

import java.io.FileReader;
import java.io.StringReader;
import java.io.BufferedReader;
import java.io.LineNumberReader;
import java.io.InputStreamReader;
import java.io.IOException;

import java.util.List;
import java.util.ArrayList;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;

import org.jiscinvolve.astrodabis.barberry.common.test.TestBase;

import org.jiscinvolve.astrodabis.barberry.common.data.TapTable;
import org.jiscinvolve.astrodabis.barberry.common.data.TapColumn;
import org.jiscinvolve.astrodabis.barberry.common.data.TapService;

/**
 *
 * Find and index TAP services.
 *
 * IVOA Registry of Registries
 * http://rofr.ivoa.net/cgi-bin/listSearchables.py
 *
 * NVO
 * http://nvo.stsci.edu/vor10/advancedsearch.aspx#||ddCapList=table%20service
 *
 * AstroGrid
 * http://registry.astrogrid.org/astrogrid-registry/main/index.jsp
 *
 * MSSL
 * http://msslxv.mssl.ucl.ac.uk/astrogrid-registry/
 *
 * EuroVO
 * http://registry.euro-vo.org/search.jsp
 * http://esavo.esa.int/registry/search.jsp
 *
 *
 */
public class RegHarvestTestCase
extends TestBase
    {


    //public static final String endpoint = "http://msslxv.mssl.ucl.ac.uk/astrogrid-registry/xqueryresults.jsp" ;
    public static final String endpoint = "http://registry.astrogrid.org/astrogrid-registry/main/xqueryresults.jsp" ;

    /**
     * Our debug logger.
     * 
     */
    protected static Log log = LogFactory.getLog(RegHarvestTestCase.class);

    @Before
    public void before()
        {
        log.debug("RegHarvestTestCase.before()");
        }

    @After
    public void after()
        {
        log.debug("RegHarvestTestCase.after()");
        }

    @Test
    public void test000()
    throws Exception
        {
        }

    //@Test
    public void test001()
    throws Exception
        {

        RegistryHarvester harvey = new RegistryHarvester();
        harvey.harvest(
            endpoint
            );

        for(RegistryHarvester.Service service : harvey.services())
            {
            log.debug(
                service
                );            
            }
        }

    //@Test
    public void test002()
    throws Exception
        {

        RegistryHarvester harvey = new RegistryHarvester();
        harvey.harvest(
            endpoint
            );

        for(RegistryHarvester.Service regService : harvey.services())
            {
            log.debug(
                regService
                );

            try {
                TapService tapService = womble().services().create(
                    new URI(
                        regService.ivorn()
                        ),
                    regService.title()
                    );
                if (tapService.name() == null)
                    {
                    tapService.name(
                        regService.title()
                        );
                    }
                if (tapService.text() == null)
                    {
                    tapService.text(
                        regService.title()
                        );
                    }
                if (tapService.endpoint() == null)
                    {
                    try {
                        tapService.endpoint(
                            new URL(
                                regService.tap().url()
                                )
                            );
                        }
                    catch (Exception ouch)
                        {
                        }
                    }
                womble().services().update(
                    tapService
                    );

                log.debug(
                    tapService
                    );

                for (RegistryHarvester.Service.Schema regSchema : regService.schemas())
                    {
                    log.debug(
                        regSchema
                        );
                    for (RegistryHarvester.Service.Schema.Table regTable : regSchema.tables())
                        {
                        log.debug(
                            regTable
                            );
                        TapTable tapTable = tapService.tables().create(
                            regTable.name()
                            );
                        log.debug(
                            tapTable
                            );

                        for (RegistryHarvester.Service.Schema.Table.Column regColumn : regTable.columns())
                            {
                            log.debug(
                                regColumn
                                );
                            TapColumn tapColumn = tapTable.columns().create(
                                regColumn.name()
                                );
                            log.debug(
                                tapColumn
                                );
                            }
                        }
                    }
                }
            catch (Exception ouch)
                {
                log.debug("Error during registry scan [" + ouch + "]");
                }
            }
        }
    }

